﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using Elasticsearch.Net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nest;
using EP.Base.BaseService.Manager.EsSystemData;
using EP.Base.BaseService.Manager.Interface;

namespace EP.Base.BaseService.Manager.Test
{
    [TestClass]
    public class UnitTest1
    {
        /// <summary>
        /// 创建一批测试索引
        /// </summary>
        [TestMethod]
        public void CreateEsIndexData()
        {
            const string hosts = "localhost:9200";
            var nodes = hosts.Split(',').Select(n => new Node(new Uri("http://"+n)));
            var pool = new StaticConnectionPool(nodes);

            var settings = new ConnectionSettings(pool).DefaultFieldNameInferrer(n => n);

            var client= new ElasticClient(settings);

            string[] indexPres =
            {
                "ep.base.baseservice.manager.es_a",
                "ep.base.baseservice.manager.es_b",
                //"ep.base.baseservice.manager.es_c",
                //"ep.base.baseservice.manager.es_d",
            };

            
            //创建按月存储格式的索引
            foreach (var indexPre in indexPres)
            {
                var beginDate = new DateTime(2016, 1, 1);
                while (beginDate <= DateTime.Now)
                {
                    string indexName = indexPre + "-" + beginDate.ToString("yyyy.MM");
                    
                    client.CreateIndex(indexName,n=>n.Settings(st=>st.NumberOfShards(1).NumberOfReplicas(1)));
                    
                    client.Alias(n => n.Add(a => a.Alias(indexPre).Index(indexName)));

                    client.Index(new BaseServiceManagerTestModel { IndexName = indexName,InserTime = DateTime.Now},
                        f => f.Index(indexName).Type("BaseServiceManagerTestModel"));

                    beginDate=beginDate.AddMonths(1);
                }
            }
        }



        /// <summary>
        /// 删除创建的索引
        /// </summary>
        [TestMethod]
        public void DeleteEsIndexData()
        {
            const string hosts = "localhost:9200";
            var nodes = hosts.Split(',').Select(n => new Node(new Uri("http://"+n)));
            var pool = new StaticConnectionPool(nodes);

            var settings = new ConnectionSettings(pool).DefaultFieldNameInferrer(n => n);

            var client = new ElasticClient(settings);

            string[] indexPres =
            {
                "ep.base.baseservice.manager.es_a",
                "ep.base.baseservice.manager.es_b",
                //"ep.base.baseservice.manager.es_c",
                //"ep.base.baseservice.manager.es_d",
            };


            //创建按月存储格式的索引
            foreach (var indexPre in indexPres)
            {
                var result = client.GetAlias(a => a.Name(indexPre));
                if (result != null && result.Indices != null)
                    foreach (var index in result.Indices.Select(index => index.Key))
                    {
                        client.DeleteIndex(index);
                    }
            }
        }

        /// <summary>
        /// 删除创建的索引
        /// </summary>
        [TestMethod]
        public void ExtractIndexNameDate()
        {
            string indexName = "ep.base.baseservice.manager.es_b-2017.05";
            string regStr = @"(?<year>\d{4})\.(?<day>\d{2})";
            Regex reg = new Regex(regStr);
            MatchCollection mc = reg.Matches(indexName);
            foreach (Match m in mc)
            {
                Console.WriteLine(m.Groups["year"].ToString());
                Console.WriteLine(m.Groups["day"].ToString());
                Console.WriteLine("------------------------------");
            }

             indexName = "ep.base.baseservice.manager.es_b-201705";
             regStr = @"(?<year>\d{4})(?<day>\d{2})";
             reg = new Regex(regStr);
             mc = reg.Matches(indexName);
            foreach (Match m in mc)
            {
                Console.WriteLine(m.Groups["year"].ToString());
                Console.WriteLine(m.Groups["day"].ToString());
                Console.WriteLine("------------------------------");
            }
        }
        public class BaseServiceManagerTestModel
        {
            public string IndexName { get; set; }
            public DateTime InserTime { get; set; }
        }
        [TestMethod]
        public void GetAlise()
        {
            IEsData esData = new EsData();
            var indexs= esData.GetAllIndexByIndexPreFixOrAlias(new string[] { "192.168.87.11:9700", "192.168.87.13:9700" }, "membersite_newsofferprice_index");
        }
    }
}
